package com.ffcs.config;

import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.stereotype.Component;
import org.springframework.util.MultiValueMap;
import org.springframework.util.StringUtils;
import org.springframework.web.server.ResponseStatusException;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;
/**
 * @author 天真热
 * @create 2024-02-01 21:37
 * @desc
 **/
@Component
public class GlobalGatewayFilter implements GlobalFilter {

    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        String path = exchange.getRequest().getPath().value();
        MultiValueMap<String, String> params =exchange.getRequest().getQueryParams();
        System.out.println("请求地址：" + path);

        ServerHttpRequest request = exchange.getRequest();
        HttpHeaders headers = request.getHeaders();
        // 检查Token是否存在
        String token = headers.getFirst("Authorization");

        if(StringUtils.isEmpty(token)){
            throw new ResponseStatusException(HttpStatus.UNAUTHORIZED, "token丢了");
        }
        return chain.filter(exchange);
    }
}
